package com.toman.toolbar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.toman.R;
import com.toman.Util.IntentUtils;
import com.toman.Util.NetworkUtils;
import com.toman.Util.ToastUtil;
import com.toman.bean.ItemListBean;
import com.toman.widget.callback.EmptyCallback;
import com.toman.widget.callback.ErrorCallback;
import com.toman.widget.callback.PostUtil;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.kingja.loadsir.core.Transport;

import java.util.List;

/**
 * Created by 邹苏隆 on 2018/7/26.
 * description：
 */

public abstract class LoadingEmptyActivity extends ToolBarActivity {
    boolean isFirst = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected abstract Activity getContent();

    public void backClick() {
        finish();
    }

    public abstract void load();

    public void initLoad() {
        loadService = LoadSir.getDefault().register(getContent(), new Callback.OnReloadListener() {
            @Override
            public void onReload(final View v) {
                // Your can change the status out of Main thread.
                new Thread(new Runnable() {
                    @Override
                    public void run() {
//                        loadService.showCallback(LoadingCallback.class);
                        //do retry logic...
//                        load();

                        //callback
                    }
                }).start();
            }
        }).setCallBack(EmptyCallback.class, new Transport() {
            @Override
            public void order(Context context, View view) {
                TextView mTvEmpty = (TextView) view.findViewById(R.id.tv_empty);
                mTvEmpty.setText("您还未添加" + mToolBarHelper.getTvTitle() + "~");
                Button mButton = (Button) view.findViewById(R.id.btn_retry);
                TextView tv_toolbar_title = (TextView) view.findViewById(R.id.tv_toolbar_title);
                tv_toolbar_title.setText(mToolBarHelper.getTvTitle());
                mButton.setText("点击返回");
                Toolbar mToolbar = (Toolbar) view.findViewById(R.id.id_tool_bar);
                mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
                mButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        load();
                    }
                });
            }
        }).setCallBack(ErrorCallback.class, new Transport() {
            @Override
            public void order(Context context, View view) {
                TextView mTvEmpty = (TextView) view.findViewById(R.id.tv_empty);
//                mTvEmpty.setText("您还未添加"+mToolBarHelper.getTvTitle()+"~");
                Button mButton = (Button) view.findViewById(R.id.btn_retry);
                TextView tv_toolbar_title = (TextView) view.findViewById(R.id.tv_toolbar_title);
                tv_toolbar_title.setText(mToolBarHelper.getTvTitle());
//                mButton.setText("点击返回");
                Toolbar mToolbar = (Toolbar) view.findViewById(R.id.id_tool_bar);

                mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
                mButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Intent intent = getIntent();
                        intent.setClass(getContent(), getContent().getClass());
                        startActivity(intent);

                        finish();
                    }
                });
            }
        });
        if (!NetworkUtils.checkNetState(getContent())) {
            PostUtil.postCallbackDelayed(loadService, ErrorCallback.class);

        }
    }


    public void initLoad(int i) {
        loadService = LoadSir.getDefault().register(getContent(), new Callback.OnReloadListener() {
            @Override
            public void onReload(final View v) {
                // Your can change the status out of Main thread.
                new Thread(new Runnable() {
                    @Override
                    public void run() {
//                        loadService.showCallback(LoadingCallback.class);
                        //do retry logic...
//                        load();
                        //callback
                    }
                }).start();
            }
        }).setCallBack(EmptyCallback.class, new Transport() {
            @Override
            public void order(Context context, View view) {
                TextView mTvEmpty = (TextView) view.findViewById(R.id.tv_empty);
                mTvEmpty.setText("您还未添加" + mToolBarHelper.getTvTitle() + "~");
                Button mButton = (Button) view.findViewById(R.id.btn_retry);
                TextView tv_toolbar_title = (TextView) view.findViewById(R.id.tv_toolbar_title);
                tv_toolbar_title.setText(mToolBarHelper.getTvTitle());
                mButton.setText("点击添加");
                Toolbar mToolbar = (Toolbar) view.findViewById(R.id.id_tool_bar);
                mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
                mButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        load();
                    }
                });
            }
        }).setCallBack(ErrorCallback.class, new Transport() {
            @Override
            public void order(Context context, View view) {
                TextView mTvEmpty = (TextView) view.findViewById(R.id.tv_empty);
                Button mButton = (Button) view.findViewById(R.id.btn_retry);
                TextView tv_toolbar_title = (TextView) view.findViewById(R.id.tv_toolbar_title);
                tv_toolbar_title.setText(mToolBarHelper.getTvTitle());
                Toolbar mToolbar = (Toolbar) view.findViewById(R.id.id_tool_bar);
                mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
                mButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Intent intent = getIntent();
                        intent.setClass(getContent(), getContent().getClass());
                        startActivity(intent);

                        finish();
                    }
                });
            }
        });
        if (!NetworkUtils.checkNetState(getContent())) {
            PostUtil.postCallbackDelayed(loadService, ErrorCallback.class);

        }
    }

    //返回LIST结果为0，跳转无网络页面
    public void isEmpty(List list, TextView mMangeNodata) {
        if (list.size() == 0) {
            mMangeNodata.setVisibility(View.VISIBLE);
        } else {
            mMangeNodata.setVisibility(View.GONE);
        }

        if (list.size() == 0 && isFirst) {
            PostUtil.postCallbackDelayed(loadService, EmptyCallback.class);
        } else {
            loadService.showSuccess();
        }

        isFirst = false;
    }
}
